﻿using System;
using System.Collections.Generic;
using Moq;
using NUnit.Framework;
using Xenta.Entities;
using Xenta.Enums;
using Xenta.Operations;
using Xenta.Plugins;
using Xenta.Structs;

namespace Xenta.Tests.Operations
{
    [TestFixture]
    public class FileGroupTests : OperationTest
    {
        protected override IEnumerable<Type> ModelMappings
        {
            get
            {
                return NHMappings.ALL;
            }
        }

        protected override IDictionary<DependencyInfo, IDependency> Dependencies
        {
            get
            {
                var components = new Dictionary<DependencyInfo, IDependency>();
                var fileStorageMock = new Mock<IFileStorage>();
                fileStorageMock.Setup(x => x.Create(It.IsAny<FileEntity>()));
                components.Add(new DependencyInfo
                {
                    ID = "52fa95dc-61b3-47b8-af7d-9453d8269d30"
                }, fileStorageMock.Object);
                return components;
            }
        }

        [Test]
        public void Can_CRUD()
        {
            #region Prepare

            var country = Processor.Execute(new CreateCountry
            {
                Code = "xy"
            }).Result;
            var localizationProfile = Processor.Execute(new CreateLocalizationProfile
            {
                Code = "xy",
                LanguageID = Processor.Execute(new CreateLanguage
                {
                    Code = "xy"
                }).Result.EntityID,
                TimeZoneID = Processor.Execute(new CreateTimeZone
                {
                    Code = "xy"
                }).Result.EntityID,
                CurrencyID = Processor.Execute(new CreateCurrency
                {
                    Code = "xy",
                    Rate = 1
                }).Result.EntityID,
                CountryID = country.EntityID,
                RegionID = Processor.Execute(new CreateRegion
                {
                    Code = "xy",
                    CountryID = country.EntityID
                }).Result.EntityID,
                DimensionUnitID = Processor.Execute(new CreateMeasureUnit
                {
                    Code = "xy-dim",
                    Ratio = 1,
                    Flags = MeasureUnitFlags.Dimension
                }).Result.EntityID,
                WeightUnitID = Processor.Execute(new CreateMeasureUnit
                {
                    Code = "xy-weight",
                    Ratio = 1,
                    Flags = MeasureUnitFlags.Weight
                }).Result.EntityID,
                InformationUnitID = Processor.Execute(new CreateMeasureUnit
                {
                    Code = "xy-info",
                    Ratio = 1,
                    Flags = MeasureUnitFlags.Information
                }).Result.EntityID
            }).Result;
            var account = Processor.Execute(new CreateAccount
            {
                Email = "email@example.com",
                Username = "username",
                Password = "password",
                LocalizationProfileID = localizationProfile.EntityID
            }).Result;
            var owner = Processor.Execute(new CreateFileOwner
            {
                AccountID = account.EntityID,
                Quota = 1
            }).Result;

            #endregion

            var group = Processor.Execute(new CreateFileGroup
            {
                OwnerID = owner.EntityID
            }).Result;

            Assert.NotNull(group);
            group.AreEqual(Processor.Execute(new GetFileGroup
            {
                EntityID = group.EntityID
            }).Result);

            var upd_group = Processor.Execute(new UpdateFileGroup
            {
                EntityID = group.EntityID,
                Flags = FileGroupFlags.Active
            }).Result;

            Assert.NotNull(upd_group);
            upd_group.AreEqual(group);
            upd_group.Flags.AreEqual(FileGroupFlags.Active);

            Processor.Execute(new DeleteFileGroup
            {
                EntityID = group.EntityID
            });
        }

        [Test]
        public void Can_Manage_Group_Relations()
        {
            #region Prepare

            var country = Processor.Execute(new CreateCountry
            {
                Code = "xy"
            }).Result;
            var localizationProfile = Processor.Execute(new CreateLocalizationProfile
            {
                Code = "xy",
                LanguageID = Processor.Execute(new CreateLanguage
                {
                    Code = "xy"
                }).Result.EntityID,
                TimeZoneID = Processor.Execute(new CreateTimeZone
                {
                    Code = "xy"
                }).Result.EntityID,
                CurrencyID = Processor.Execute(new CreateCurrency
                {
                    Code = "xy",
                    Rate = 1
                }).Result.EntityID,
                CountryID = country.EntityID,
                RegionID = Processor.Execute(new CreateRegion
                {
                    Code = "xy",
                    CountryID = country.EntityID
                }).Result.EntityID,
                DimensionUnitID = Processor.Execute(new CreateMeasureUnit
                {
                    Code = "xy-dim",
                    Ratio = 1,
                    Flags = MeasureUnitFlags.Dimension
                }).Result.EntityID,
                WeightUnitID = Processor.Execute(new CreateMeasureUnit
                {
                    Code = "xy-weight",
                    Ratio = 1,
                    Flags = MeasureUnitFlags.Weight
                }).Result.EntityID,
                InformationUnitID = Processor.Execute(new CreateMeasureUnit
                {
                    Code = "xy-info",
                    Ratio = 1,
                    Flags = MeasureUnitFlags.Information
                }).Result.EntityID
            }).Result;
            var account = Processor.Execute(new CreateAccount
            {
                Email = "email@example.com",
                Username = "username",
                Password = "password",
                LocalizationProfileID = localizationProfile.EntityID
            }).Result;
            var owner = Processor.Execute(new CreateFileOwner
            {
                AccountID = account.EntityID,
                Quota = 2
            }).Result;

            #endregion

            var group = Processor.Execute(new CreateFileGroup
            {
                OwnerID = owner.EntityID
            }).Result;
            var file = Processor.Execute(new CreateFile
            {
                FolderID = 0,
                OwnerID = owner.EntityID,
                StorageID = Guid.Parse("52fa95dc-61b3-47b8-af7d-9453d8269d30"),
                Name = "name",
                MimeType = "mimeType",
                Size = 1
            }).Result;

            Processor.Execute(new MapFileToGroup
            {
                EntityID = group.EntityID,
                ChildID = file.EntityID
            }).Result.Files
            .DoesContain(file);

            Processor.Execute(new UnmapFileFromGroup
            {
                EntityID = group.EntityID,
                ChildID = file.EntityID
            }).Result.Files
            .DoesNotContain(file);
        }
    }
}
